package dgut.bbm.service.impl;

import com.alibaba.fastjson.JSON;
import dgut.bbm.common.utils.HttpUtils;
import dgut.bbm.to.FriendTo;
import dgut.bbm.to.UserLoginTo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dgut.bbm.common.utils.PageUtils;
import dgut.bbm.common.utils.Query;

import dgut.bbm.dao.UserDao;
import dgut.bbm.entity.UserEntity;
import dgut.bbm.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserEntity login(String code) throws Exception {
        UserEntity userEntity=null;
        //TODO
//        String appid = "wx67a0a4e711efacfb";
//        String secret = "d81a996331980bcc27366a50bfcf5442";
        String appid = "wx8819aafb70ef26f7";
        String secret = "9fcefa27b943b2bbbedae4f5aab1697e";
        //appid和secret是开发者分别是小程序ID和小程序密钥，开发者通过微信公众平台-》设置-》开发设置就可以直接获取，
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="
                + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
        System.out.println(code);
        Map<String, String> map = new HashMap<>();
        map.put("grant_type", "authorization_code");
        map.put("js_code", code);
        map.put("appid", appid);
        map.put("secret", secret);

        //1、根据用户授权返回的code换取access_token
        HttpResponse response = HttpUtils.doGet("https://api.weixin.qq.com", "/sns/jscode2session", "get", new HashMap<String, String>(), map);

        //2、处理
        if (response.getStatusLine().getStatusCode() == 200) {
            //返回封装
            String json = EntityUtils.toString(response.getEntity());
            UserLoginTo userto = JSON.parseObject(json, UserLoginTo.class);
            System.out.println(json);
            System.out.println(userto.getOpenid());
            userEntity=this.baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("wx_id", userto.getOpenid()));
            if(userto.getOpenid()!=null){
                if (userEntity==null){
                    //没注册注册入数据库
                    userEntity=register(userto);
                }
            }
            //注册过直接返回
        }
//        userEntity=this.baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("id",1));
        return userEntity;
    }



    public UserEntity register(UserLoginTo userTo){

        UserEntity userEntity=new UserEntity();
//        userEntity.setWxId(userTo.getOpenid());
        userEntity.setWxId(userTo.getOpenid());
        this.baseMapper.insert(userEntity);
        return userEntity;
    }
}